module.exports = class extends think.Model {
  async getAction(params) {
    const data = this.model(params.model).where(params.data).find();
    return data;
  }

  // findByIdAction(params) {
  //   let data;
  //   data = this.model(params.model).where({ id: params.id }).find();
  //   return data;
  // }

  async listallAction(params) {
    let data;
    var arr = Object.keys(params.data);// 传空返回全部数据，不为空选择查询
    if (arr.length) {
      data = await this.model(params.model).where({...params.data}).select();
      return data.reverse();
    } else {
      data = await this.model(params.model).select();
      return data.reverse();
    }
  }

  async delAction(params) {
    const data = this.model(params.model).where(params.data).delete();
    return data;
  }

  async editAction(params) {
    const data = this.model(params.model).where({ id: params.data.id }).update(params.data);
    return data;
  }

  async addAction(params) {
    const data = this.model(params.model).add(params.data);
    return data;
  }

  async listpageAction(params) {
    const DATA = {};
    const NEWdDATA = {};
    for (const i in params.data) {
      DATA[i] = params.data[i];
    }
    for (const i in DATA) {
      NEWdDATA[i] = DATA[i];
    }
    delete NEWdDATA.page;
    delete NEWdDATA.pageSize;
    const data = await this.model(params.model).where(NEWdDATA).page(DATA.page || 1, DATA.pageSize || 20).countSelect();
    const databack = {};
    databack.pageSize = data.pageSize;
    databack.current = data.currentPage;
    databack.pages = data.totalPages;
    databack.total = data.count;
    databack.data = data.data;
    return databack;
  }
};
